package com.codesuc.aichat.chatHistory.controller;

import com.codesuc.aichat.chatHistory.entity.ChatHistory;
import com.codesuc.aichat.chatHistory.service.ChatHistoryService;
import com.codesuc.aichat.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Author: ZhangGongMing
* @CreateTime: 2025/09/23 12:56
* @Description: ChatHistory控制层
* @Version: 1.0
*/
@RestController
@RequestMapping(value = "/api/v1/chatHistory")
@Tag(name = "ChatHistoryController", description = "ChatHistory控制层")
public class ChatHistoryController {

    @Autowired
    private ChatHistoryService chatHistoryService;

    /**
     * 根据用户ID和角色ID查询聊天记录
     */
    @GetMapping("/list")
    @Operation(summary = "根据用户ID和角色ID查询聊天记录")
    public Result<List<ChatHistory>> getChatHistory(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "角色ID", required = true) @RequestParam Long roleId) {
        List<ChatHistory> chatHistoryList = chatHistoryService.getChatHistoryByUserIdAndRoleId(userId, roleId, false);
        return Result.ok(chatHistoryList);
    }

    /**
     * 根据用户ID和角色ID分页查询聊天记录
     */
    @GetMapping("/page")
    @Operation(summary = "根据用户ID和角色ID分页查询聊天记录")
    public Result<Page<ChatHistory>> getChatHistoryPage(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "角色ID", required = true) @RequestParam Long roleId,
            @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") Integer pageNo,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ChatHistory> chatHistoryPage = chatHistoryService.getChatHistoryByUserIdAndRoleIdPage(userId, roleId, pageNo, pageSize);
        return Result.ok(chatHistoryPage);
    }

    /**
     * 新增聊天记录
     */
    @PostMapping("/add")
    @Operation(summary = "新增聊天记录")
    public Result<String> addChatHistory(@RequestBody ChatHistory chatHistory) {
        boolean success = chatHistoryService.save(chatHistory);
        if (success) {
            return Result.ok("新增聊天记录成功");
        } else {
            return Result.error("新增聊天记录失败");
        }
    }

}
